// pages/comment/comment.js
import { getSingleCommentList, getCommentList, addComment } from "../../utils/api.js";
import { retTimeInterval } from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vid: '',
    cid: 0,
    cidData: false,
    vidData: false,
    comment: '',
    loadingPage: 1,
    loadingNum: 12,
    replyCount: 12,
    cidReplyNum: 12
  },
  getCidComment: function (isCloseLoading){
    getSingleCommentList(this.data.cid, this.data.replyCount, this.data.loadingPage, (data)=>{
      if (isCloseLoading) {
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
        // 隐藏加载动画
        wx.hideLoading();
      }
      if (data.length == 0 ) {
        console.log("没了, 兄dei");
        return;
      }
      data.interval = retTimeInterval(data.pubtime);
      var cLike = wx.getStorageSync('commentLike');
      data.liked = cLike[data.cid] ? cLike[data.cid] : 'false';
      var origin = this.data.cidData.replys;
      if (origin && this.data.loadingPage != 1) {
        data.replys.forEach((item) => {
          origin.push(item);
        });
        this.setData({
          cidData: this.data.cidData
        })
      } else {
        this.setData({
          cidData: data
        });
      }
    })
  },
  getVidComment: function (isCloseLoading){
    getCommentList(this.data.vid, this.data.loadingNum, this.data.loadingPage, (data)=>{
      // console.log(data)
      if (isCloseLoading) {
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
        // 隐藏加载动画
        wx.hideLoading();
      }
      if (data.length == 0 ){
        console.log("没了, 兄dei");
        return;
      }
      var cLike = wx.getStorageSync('videoLike');
      data.forEach((item)=>{
        item.interval = retTimeInterval(item.pubtime);
        item.liked = cLike[data.cid]? cLike[item.vid] : 'false'
      })
      console.log(this.data.vidData);
      var origin = this.data.vidData;
      console.log(origin)
      if (origin && this.data.loadingPage != 1) {
        console.log("哟数据")
        data.forEach((item) => {
          origin.push(item);
        });
        this.setData({
          vidData: origin
        })
      } else {
        this.setData({
          vidData: data
        });
      }
    })
  },
  inputEvent: function (e) {
    // console.log(e);
    this.setData({
      comment: e.detail.value
    })
  },
  formSubmit: function (e) {
    // 检查登录状态
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          var reg = /^\s*\s+$/g;
          var input = this.data.comment;
          // console.log(this.data);
          if (input.replace(reg, '') == "") {
            wx.showModal({
              title: '提示',
              content: '输入内容不可以为空哦',
              showCancel: false
            })
          } else {
            var commentid = this.data.vid || this.data.cidData.vid;
            var topicid = this.data.cidData.cid || 0;
            var toopenid = this.data.cidData.openid || '';
            var openid = wx.getStorageSync('openid');
            addComment(commentid, topicid, openid, toopenid, input, (data) => {
              if (data.errmsg === "success" && data.errcode === 0) {
                if (this.data.cid) {
                  this.getCidComment()
                } else {
                  this.getVidComment()
                }
                this.setData({
                  comment: ""
                })
              } else {
                console.log("发表评论失败", data);
              }
            })
          }
        }else{
          wx.showModal({
            title: '授权提示',
            content: '对不起, 要使用评论功能需要授权公开您的信息, 您可以点击主页左上角的头像,引导授权',
            confirmText: "返回主页",
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/video/video',
                });
              }
            }
          })
        }
      }.bind(this)
    })
    
  },
  toCommentPage: function (e) {
    wx.navigateTo({
      url: '/pages/comment/comment?cid=' + e.currentTarget.dataset.topicid
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.vid){
      this.setData({
        vid: options.vid
      })
    }else if(options.cid){
      this.setData({
        cid: options.cid
      })
    }else{
      console.log("参数错误");
    }
  },
  getData: function (isCloseLoading) {
    if (this.data.vid) {
      this.getVidComment(isCloseLoading)
    } else if (this.data.cid) {
      this.getCidComment(isCloseLoading)
    } else {
      console.log("参数错误");
    };
  },
  onShow: function(){
    // console.log("触发");
    this.getData();
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.getData(true)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中...',
    });
    var temp = this.data.loadingPage;
    temp++;
    this.setData({
      loadingPage: temp
    })
    this.getData(true);
  }
})